/*
 * For legal stuff see the file COPYRIGHT
 */
#import "ExpenseViewMgr.h"
#import "ClientInspector.h"
#import "ExpenseEditor.h"
#import "Controller.h"

@interface ExpenseViewMgr(PRIVATE)
- (void)displayTotalExpenses;
- (void)displayClientExpenses;
- (float)totalExpenses;
@end


@implementation ExpenseViewMgr

- itemEditor
{
  return [ExpenseEditor new];
}

/*
 * Provide the add, delete and sort methods to invoke on ClientInfo objects
 */
- (SEL)addMethod
{
  return @selector(addExpense:);
}

- (SEL)deleteMethod
{
  return @selector(deleteExpense:);
}

- (SEL)sortMethod
{
  return @selector(sortExpenses);
}

- info:(ClientInfo *)info itemAt:(int)position
{
  return [info expenseAt:position];
}

- (int)itemCount:(ClientInfo *)info 
{
  return [info expenseCount];
}

- (void)displaySummary
{
  [self displayClientExpenses];
  [self displayTotalExpenses];
}

@implementation ExpenseViewMgr(PRIVATE)

- (float)totalExpenses
{
  List *clientList = [[NXApp delegate] clientList];
  int i, count = [clientList count];
  float totalExp = 0.0;

  for ( i = 0; i < count; i++ )
    totalExp += [[clientList objectAt:i] totalExpenses];

  return totalExp;
}

- (void)displayClientExpenses
{
  char buf[20];
  id info = [[ClientInspector sharedInstance] selectedClient];

  if ( info ) {
    sprintf( buf, "%.2f", [info totalExpenses] );
    [clientExpensesField setStringValue:buf];
  } else
    [clientExpensesField setStringValue:""];
}

- (void)displayTotalExpenses
{
  char buf[20];

  sprintf( buf, "%.2f", [self totalExpenses] );
  [totalExpensesField setStringValue:buf];
}

@end
 
